﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Drawing;

namespace Touchscreen
{
    public partial class Stap4 : System.Web.UI.Page
    {
        private Voorstelling v;
        private string totaal;

        protected void Page_Load(object sender, EventArgs e)
        {
            v = (Voorstelling)Session["voorstelling"];

            if (v == null)
            {
                Response.Redirect("Stap1.aspx");
            }
            else
            {
                initComponents();
            }
            if (!IsPostBack)
            {
                pincode.Attributes.Add("readonly", "readonly");
            }
        }

        private void initComponents()
        {
            Master.labelExplain.Text = "Afrekenen";
            Movie movie = (Movie)LoadControl("Movie.ascx");
            movie.voorstelling = v;
            Panel.Controls.Add(movie);

            Button b = new Button();
            b.Text = "Annuleren";
            b.BackColor = Color.Red;
            b.CssClass = "ButtonCancelPlaceHolder";
            b.PostBackUrl = "Stap1.aspx";
            movie.panelParentGuide.Controls.Add(b);

            totaal = String.Format("{0:0.00}", Session["totalprice"]);
            LabelTotalPrice.Text = totaal;
        }

        //De enter toets van het pinbord om de code door te sturen naar de API.
        protected void button_enter(object sender, EventArgs e)
        {
            pinapi(pincode.Text);
        }

        //De pin API uitlezen en gebruiken.
        public void pinapi(string pincode)
        {
            Boolean success = false;

            try
            {
                System.Net.HttpWebRequest fr;
                Uri targetUri = new Uri("http://aib.aibbreda.nl/betaal.php?bedrag=" + totaal.Replace(",",".") + "&pincode=" + pincode);
                fr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(targetUri);
                if ((fr.GetResponse().ContentLength > 0))
                {
                    System.IO.StreamReader str = new System.IO.StreamReader(fr.GetResponse().GetResponseStream());

                    //Controleren of de reactie van het script positief of negatief is.
                    if (str.ReadToEnd().Substring(0, 6) == "SUCCES")
                    {
                        //succes
                        success = true;
                    }
                    else
                    {
                        //error
                        Response.Write("<script>alert('Pincode onjuist, probeer het nog een keer!')</script>");           
                    }

                    if (str != null)
                    {
                        str.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + "Oeps! er ging iets mis: " + ex.Message + "')</script>");
            }

            if (success)
            {
                Module.database.SubmitChanges();
                Response.Redirect("Stap5.aspx");
            }
        }
    }
}